package com.github.library.screenShot;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by calvin on 12/13/14.
 */
public class ClipShadowView extends View {
    private int mHorizontalPadding = 20;
    private int mVerticalPadding;
    private int mWidth;
    private int mBorderColor = Color.parseColor("#FFFFFF");
    private int mBorderWidth = 1;
    private Paint mPaint;

    public ClipShadowView(Context context) {
        super(context);
    }

    public ClipShadowView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mBorderWidth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, mBorderWidth, getResources()
                        .getDisplayMetrics());
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    public ClipShadowView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(getShadowImageView(getWidth(), getHeight()), 0, 0, mPaint);
    }

    private Bitmap getShadowImageView(int width, int height) {
        Bitmap bitMap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitMap = bitMap.copy(bitMap.getConfig(), true);
        Canvas canvas = new Canvas(bitMap);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.parseColor("#aa000000"));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawRect(0, 0, width, height, paint);

        Paint circle = new Paint();
        circle.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        circle.setAntiAlias(true);
        circle.setColor(Color.TRANSPARENT);
        circle.setStyle(Paint.Style.FILL);
        canvas.drawCircle(width / 2, height / 2, width / 2 - mHorizontalPadding, circle);

        return bitMap;
    }

    public void setHorizontalPadding(int mHorizontalPadding) {
        this.mHorizontalPadding = mHorizontalPadding;
    }
}
